package com.ziyan.controller;

import com.ziyan.entity.URLEntity;
import com.ziyan.service.URLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Driver;

/**
 * Created by ZMy on 2017/5/24.
 */
@Controller
@RequestMapping("/")
public class RedirectController {
    @Autowired
    private URLService urlService;

    @GetMapping("/{key}")
    public void redirect(@PathVariable String key, HttpServletResponse response) throws IOException {
        URLEntity urlEntity = urlService.findByKeyWord(key);
        if (urlEntity != null) {
            response.sendRedirect(urlEntity.getUrl());
        }
    }
}
